import axios from 'axios'
// import router from '@/router'
import { showLoading, hideLoading } from './loading'

const isProd = process.env.NODE_ENV === 'production'
const BASE_URL = isProd ? 'http://localhost/shopAdmin/public/vueapi' : '/api'

const instance = axios.create({
  baseURL: BASE_URL, // api的BASE_URL
  timeout: 8000 // 请求超时时间
})

instance.interceptors.request.use(
  config => {
    if (config.showLoading) {
      showLoading()
    }
    // if (store.getters.token) {
    //   config.headers['X-Token'] = getToken() // 让每个请求携带自定义token 请根据实际情况自行修改
    // }
    return config
  },error => {
    // Do something with request error
    console.log(error) // for debug
    Promise.reject(error)
  }
)


instance.interceptors.response.use(
  res => {
    hideLoading()
    return res.data
  },
  err => {
    hideLoading()
    return Promise.reject(err)
  }
)

export default instance
